package com.example.cofeed;

import android.Manifest;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.PhoneUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.example.cofeed.activity.GuideActivity;
import com.example.cofeed.activity.LoginActivity;
import com.example.cofeed.activity.PinZhongActivity;
import com.example.cofeed.activity.SupplyDemandActivity;
import com.example.cofeed.activity.WebViewActivity;
import com.example.cofeed.api.ApiService;
import com.example.cofeed.base.BaseActivity;
import com.example.cofeed.callback.DialogCallback;
import com.example.cofeed.callback.LzyResponse;
import com.example.cofeed.constant.Constants;
import com.example.cofeed.constant.IntentKeyConstant;
import com.example.cofeed.constant.SputilsConstant;
import com.huawei.android.hms.agent.HMSAgent;
import com.huawei.android.hms.agent.common.handler.ConnectHandler;
import com.huawei.android.hms.agent.push.handler.GetTokenHandler;
import com.lzy.okgo.model.Response;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import me.weyye.hipermission.HiPermission;
import me.weyye.hipermission.PermissionCallback;
import me.weyye.hipermission.PermissionItem;

/**
 * Created by mqwang on 2017/9/28.
 */

public class SplashActivity extends BaseActivity {
    @Override
    protected int getLayoutId() {
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        return R.layout.activity_splash;
    }

    @Override
    protected void init(Bundle savedInstanceState, View container) {

        if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) > 0){
            finish();
            return;
        }
        String brand= Build.BRAND.toLowerCase();
        if (brand.equals("huawei")){
            HMSAgent.connect(this, new ConnectHandler() {
                @Override
                public void onConnect(int rst) {
                    HMSAgent.Push.getToken(new GetTokenHandler() {
                        @Override
                        public void onResult(int rtnCode) {
                        }
                    });
                }
            });
        }

        if (!SPUtils.getInstance().getBoolean("dialog", false)){
            showDialog();
            SPUtils.getInstance().put("dialog", true);
        }else {
            //... To-do
            List<PermissionItem> permissionItems = new ArrayList<PermissionItem>();
            permissionItems.add(new PermissionItem(android.Manifest.permission.WRITE_EXTERNAL_STORAGE, "读写权限", R.drawable.permission_ic_storage));
            permissionItems.add(new PermissionItem(android.Manifest.permission.READ_PHONE_STATE, "电话权限", R.drawable.permission_ic_phone));
            permissionItems.add(new PermissionItem(Manifest.permission.CAMERA, "拍照权限", R.drawable.permission_ic_camera));
            HiPermission.create(SplashActivity.this)
                    .title("提示")
                    .permissions(permissionItems)
                    .animStyle(R.style.PermissionAnimScale)
                    .style(R.style.PermissionDefaultNormalStyle)
                    .checkMutiPermission(new PermissionCallback() {
                        @Override
                        public void onClose() {

                        }
                        @Override
                        public void onFinish() {
                            //获取剪贴板管理器：
                            ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                            // 创建普通字符型ClipData
                            ClipData mClipData = ClipData.newPlainText("Label", PhoneUtils.getIMEI());
                            // 将ClipData内容放到系统剪贴板里。
                            cm.setPrimaryClip(mClipData);

                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    if (SPUtils.getInstance().getBoolean(SputilsConstant.IS_FIRST, true)){
                                        ActivityUtils.startActivity(GuideActivity.class);
                                    }else {
                                        ActivityUtils.startActivity(SplashActivity.this, MainActivity.class);
                                    }

                                    finish();
                                }
                            }, 3000);
                        }

                        @Override
                        public void onDeny(String permission, int position) {

                        }
                        @Override
                        public void onGuarantee(String permission, int position) {

                        }
                    });
        }

    }

    private void showDialog(){

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog alertDialog = builder.create();
        alertDialog.setCancelable(false);

        View view = LayoutInflater.from(this).inflate(R.layout.dialog_yhys, null);
        alertDialog.setView(view);

        //Unable to add window android.view.ViewRootImpl$W@12b82d6 -- permission denied for this window type
        alertDialog.show();
        alertDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
            }
        });


        Button btn_unagree = view.findViewById(R.id.btn_unagree);
        Button btn_agree = view.findViewById(R.id.btn_agree);
        view.findViewById(R.id.tv_xieyi).setOnClickListener(v -> {
            Intent intent = new Intent(mContext, WebViewActivity.class);
            intent.putExtra(IntentKeyConstant.ID, -2);
            intent.putExtra(IntentKeyConstant.URL, "https://app.cofeed.com/treaty/treaty_detail");
            startActivity(intent);
        });

        btn_unagree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                //... To-do
                alertDialog.dismiss();
            }
        });

        btn_agree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //... To-do
                List<PermissionItem> permissionItems = new ArrayList<PermissionItem>();
                permissionItems.add(new PermissionItem(android.Manifest.permission.WRITE_EXTERNAL_STORAGE, "读写权限", R.drawable.permission_ic_storage));
                permissionItems.add(new PermissionItem(android.Manifest.permission.READ_PHONE_STATE, "电话权限", R.drawable.permission_ic_phone));
                permissionItems.add(new PermissionItem(Manifest.permission.CAMERA, "拍照权限", R.drawable.permission_ic_camera));
                HiPermission.create(SplashActivity.this)
                        .title("提示")
                        .permissions(permissionItems)
                        .animStyle(R.style.PermissionAnimScale)
                        .style(R.style.PermissionDefaultNormalStyle)
                        .checkMutiPermission(new PermissionCallback() {
                            @Override
                            public void onClose() {

                            }
                            @Override
                            public void onFinish() {
                                //获取剪贴板管理器：
                                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                                // 创建普通字符型ClipData
                                ClipData mClipData = ClipData.newPlainText("Label", PhoneUtils.getIMEI());
                                // 将ClipData内容放到系统剪贴板里。
                                cm.setPrimaryClip(mClipData);

                                new Handler().postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        if (SPUtils.getInstance().getBoolean(SputilsConstant.IS_FIRST, true)){
                                            ActivityUtils.startActivity(GuideActivity.class);
                                        }else {
                                            ActivityUtils.startActivity(SplashActivity.this, MainActivity.class);
                                        }

                                        finish();
                                    }
                                }, 3000);
                            }

                            @Override
                            public void onDeny(String permission, int position) {

                            }
                            @Override
                            public void onGuarantee(String permission, int position) {

                            }
                        });

                alertDialog.dismiss();
            }
        });


    }

    @Override
    protected boolean isNeedTitleBar() {
        return false;
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

}
